package com.sjsu.cloudmanager.service;

import com.sjsu.cloudmanager.dao.CloudLabDao;
import com.sjsu.cloudmanager.model.CloudLab;
import com.sjsu.cloudmanager.model.ComputingMachine;
import com.sjsu.cloudmanager.model.MachineSoftware;
import com.sjsu.cloudmanager.model.Software;
import org.apache.commons.collections.CollectionUtils;


import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * cloud lab service impl
 * User: ckempaiah
 * Date: 9/21/11
 * Time: 12:14 AM
 * To change this template use File | Settings | File Templates.
 */
public class CloudLabServiceImpl implements CloudLabService {

    private CloudLabDao cloudLabDao;
    @Override
    public Integer createCloudLab(String labName, String status){

        return cloudLabDao.createCloudLab(labName, status);
    }

    @Override
    public Integer addComputingMachine(Integer labId, String machineName, String operatingSystem, int memory, String osVersion) {

        CloudLab cloudLab = cloudLabDao.getCloudLab(labId);

        ComputingMachine computingMachine = new ComputingMachine();

        computingMachine.setCloudLab(cloudLab);
        computingMachine.setMachineName(machineName);
        computingMachine.setOperatingSystem(operatingSystem);
        computingMachine.setOperatingSystemVersion(osVersion);
        computingMachine.setMemory(memory);
        cloudLabDao.saveOrUpdate(computingMachine);
        return computingMachine.getMachineId();
    }

    @Override
    public void addMachineSoftware(Integer machineId, List<Integer> softwareIdList) {

        ComputingMachine computingMachine = cloudLabDao.getComputingMachine(machineId);

        if(CollectionUtils.isNotEmpty(softwareIdList)){
            Set<MachineSoftware> machineSoftwareSet = new HashSet<MachineSoftware>();
            for (Integer softwareId: softwareIdList) {

                MachineSoftware machineSoftware = new MachineSoftware();
                machineSoftware.setComputingMachine(computingMachine);
                Software software = cloudLabDao.getCloudSoftware(softwareId);
                machineSoftware.setSoftware(software);
                machineSoftwareSet.add(machineSoftware);
            }
            computingMachine.setMachineSoftware(machineSoftwareSet);
            cloudLabDao.saveOrUpdate(computingMachine);
        }

    }
    //injected beans
    public CloudLabDao getCloudLabDao() {
        return cloudLabDao;
    }

    public void setCloudLabDao(CloudLabDao cloudLabDao) {
        this.cloudLabDao = cloudLabDao;
    }

    @Override
    public ComputingMachine getComputingMachine(Integer machineId) {
        return cloudLabDao.getComputingMachine(machineId);
    }

    @Override
    public CloudLab getCloudLab(Integer labId) {
        return cloudLabDao.getCloudLab(labId);
    }

    @Override
    public void removeCloudLab(Integer cloudLabId) {
        CloudLab cloudLab = cloudLabDao.getCloudLab(cloudLabId);

        cloudLabDao.removeCloudLab(cloudLab);
    }

    @Override
    public void removeComputingMachine(Integer machineId) {
        ComputingMachine computingMachine = cloudLabDao.getComputingMachine(machineId);
        cloudLabDao.removeComputingMachine(computingMachine);
    }
}
